<?php

class Pawns extends Controller {
	
	function Pawns()
	{
		parent::Controller();
		$this->load->library('table');
		$this->load->model('Pawns_model');
		$this->load->model('Items_model');
		$this->load->helper('array');
	}

	function index()
	{
		// Displays the initial pawns view
		$this->load->view('pawns/main_view');
	}
	
	function show()		
	{
		// Calls the Pawns_model get() function, which returns an array of the 
		// complete pawns table. It then sends this array to the show_all_view page.
		
		$single_pawns_view = FALSE;
		
		if ($this->uri->segment(3))		// check to see if there is a $pawn_id field
		{
		   $single_pawns_view = TRUE; 
			$this->Pawns_model->pawn_id = $this->uri->segment(3);
			$this->Items_model->pawn_id = $this->Pawns_model->pawn_id;
			$data['item_result'] = $this->Items_model->get();
		}
		
		$data['result'] = $this->Pawns_model->get();
		
		$this->load->view('_global/header');
		if ($single_pawns_view)
			$this->load->view('pawns/show_single_pawn_view',$data);
		else
			$this->load->view('pawns/show_all_pawns_view',$data);	
		$this->load->view('_global/footer');
		
	}
	
	function add()		
	{
		// Adds a new pawn.
		
		// Call the initialization function
		$data=$this->Pawns_model->general();
	
		// Set defaults for a new pawn record
		$data['result']->pawn_year = date("Y");
		$data['result']->pawn_month = date("m");
		$data['result']->pawn_day = date("d");
		$data['result']->pawn_time = date("h:i");
		$data['result']->monthly_interest = 10;
		$data['result']->pawn_control_number = $this->Pawns_model->getLastPawnNumber(); 
		
		// Set the customer drop-down selection to the customer passed in the URI
		$data['fcustomerid']['select'] = $this->uri->segment(3);
		// Set the default transaction type to '3' which is 'pawn'
		$data['ftransactiontype']['select'] = '3';
	
		// Call the pawns/add_view
		$this->load->view('_global/header');
		$this->load->view('pawns/add_pawn_view',$data);	
		$this->load->view('_global/footer');	
	}
	
	function update()
	{
		// Updates an existing Pawn record
		
		// Call the initialization function
		$data=$this->Pawns_model->general();
		
		// Check to see if there is a $pawn_id field passed in the URI
		if ($this->uri->segment(3))
		{
			$this->Pawns_model->pawn_id = $this->uri->segment(3);
			$data['result'] = $this->Pawns_model->get();
			
			//	prepare lookup fields
			$data['fcustomerid']['select'] = $data['result']->customer_id;
			$data['ftransactiontype']['select'] = $data['result']->pawn_transaction_type_id;
			$data['fstatus']['select'] = $data['result']->pawn_status_id;
			
			
			// Split up the date into the month, day, and year
			// and add these new values to the $data['result'] array
			list($data['result']->pawn_year, $data['result']->pawn_month, $data['result']->pawn_day) = 													split("-",$data['result']->pawn_date);
			
			$this->load->view('_global/header');
			$this->load->view('pawns/update_pawn_view',$data);
			$this->load->view('_global/footer');
		}
		else
		{
			echo ("No Pawn id submitted!");
			echo anchor('start/mainpage','Back to Main Page');
		}
	}
	
	function save()
	{
		// Receives the add_pawn_form result.
		// Takes the year,month,day fields from the form and concatenates them into the $dob field.
		// 
		
		
		// Set the calculated fields
		$pawn_date = $this->input->post('pawn_year') . $this->input->post('pawn_month') . $this->input->post('pawn_day');
		$maturity_date = date("Ymd",date_addmonths("1",mysql_to_unix($pawn_date)));
		$finance_charge = $this->input->post('monthly_interest') * .01 * $this->input->post('pawn_amount_financed');

		// Add calculated fields to the POST variable		
		$_POST['pawn_date'] = $pawn_date;
		$_POST['maturity_date'] = $maturity_date;
		$_POST['finance_charge'] = $finance_charge;
		
		// Call the model save() function, and pass it the POST variable	
		$this->Pawns_model->save($_POST);
		
		// Get the pawn_id of the last inserted pawn record in order to pass it to the items add function
		$lastId = $this->Pawns_model->lastInsertedId();
			
		// Call the Items Add function
		redirect('items/add/'.$lastId);
	}
	
	function delete()		// delete a customer
	{
		//call the Customers model delete_Customers function
		
		if ($this->uri->segment(3))
			$this->Pawns_model->delete($this->uri->segment(3));
		
		redirect('customers/show');

	}
	
	function search()		// search for customers
	{
		if (isset($_POST['search']))
		{
			// query database with the $POSTED information
			// Call the customers/show method.
			$this->Pawns_model->pawn_control_number = $this->input->post('pawn_control_number');
			$isSearch = TRUE;
			redirect('pawns/show');
			
		}
		else
		{
			// display the search form
			$this->load->view('pawns/search_view');
		}
	}

}
?>
